﻿using System;
using System.Collections;
using System.Windows.Forms;
using NLog;


namespace StickTops
{
    public partial class MainForm : Form
    {
        public static MainForm Instance;

        #region "Private members"

        private ArrayList StickTopList = new ArrayList();
        private LoginForm loginForm = new LoginForm();
        private SaveSql Sql;
        private static Logger logger = LogManager.GetCurrentClassLogger();

        #endregion

        #region "Constructor"

        public MainForm()
        {
            InitializeComponent();

            Instance = this;
        }

        #endregion

        #region "Private Methods"

        private void MainForm_Load(object sender, EventArgs e)
        {
            logger.Debug("MainForm On Load Starts with autologin: " + Properties.Settings.Default.AutoLogin);

            if (Properties.Settings.Default.AutoLogin && Properties.Settings.Default.Username != "")
            {

                logger.Debug("Trying to AutoLogon...");

                if (Sql == null){ Sql = new SaveSql();};
                Sql.LogIn(Properties.Settings.Default.Username, Properties.Settings.Default.Password);

                if (Sql.State == SaveSql.LogInState.Success)
                {
                    Vars.UserName = Properties.Settings.Default.Username;
                    Vars.PassWord = Properties.Settings.Default.Password;
                    Vars.LoggedIn = true;
                }
                else
                {
                    loginForm.Show();
                    logger.Debug("AutoLogon failed because: " + Sql.Message);
                }
            }
            else 
            {
                logger.Debug("Starting without autologin");
                loginForm.Show();
            }
        }

        public void SetMenuInfo()
        {
            //Log out
            if (Vars.LoggedIn == false)
            {
                MenuItemInfo.Text = "Log In";
                MenuItemInfo.Enabled = true;
            }
            else
            {
                MenuItemInfo.Text = Vars.UserName;
                MenuItemInfo.Enabled = false;
            }

        }

        private void AutoSave_Tick(object sender, EventArgs e)
        {
            if (Vars.LoggedIn)
            {
                MenuItemSave_Click(this, e);
            }
        }

        private void MenuItemSettings_Click(object sender, EventArgs e)
        {
            SettingsForm setForm = new SettingsForm();
            DialogResult savingDone = setForm.ShowDialog();
            AutoSave.Interval = Properties.Settings.Default.AutoSaveIntervall;
        }

        #endregion

        #region "Public methods"

        public ArrayList GetNotes()
        {
            return StickTopList;
        }

        public void MakeNewNote(NewNote newnote)
        {
            StickTop NewStick = new StickTop(newnote);
            NewStick.Show();
            StickTopList.Add(NewStick);
            SetMenuInfo();
        }

        public void MakeNewNote()
        {
            StickTop NewStick = new StickTop(new NewNote("",Properties.Settings.Default.StartSize,
                                                            Properties.Settings.Default.StartPos,
                                                            Properties.Settings.Default.MainFore,
                                                            Properties.Settings.Default.MainBack,
                                                            Properties.Settings.Default.LabelFore,
                                                            Properties.Settings.Default.LabelBack
                                                            ));
            NewStick.Show();
            StickTopList.Add(NewStick);
        }

        public void SaveNotes(Boolean ShowResults)
        {
            //reset AutoSave timer
            AutoSave.Enabled = false; AutoSave.Enabled = true;

            if (Sql == null) { Sql = new SaveSql(); }

            bool SqlSaveStatus;

            //Save to Sql and display the result
            SqlSaveStatus = Sql.Save(Vars.UserName);

            if (SqlSaveStatus)
            {
               if (ShowResults) NotifyIcon.ShowBalloonTip(2000, "Save complete", "The notes are saved succesfully!", ToolTipIcon.Info);
            }
            else
            {
                if (ShowResults) NotifyIcon.ShowBalloonTip(2000, "Save failed", "Could not save the notes because: " + Sql.Message, ToolTipIcon.Error);
            }
            logger.Trace("MainForm Save Result: " + SqlSaveStatus.ToString());
        }

        #endregion

        #region "Menu"

        private void MenuItemInfo_Click(object sender, EventArgs e)
        {
            if (Vars.LoggedIn)
            {
                foreach (StickTop i in StickTopList)
                {
                    i.BringToFront();
                }
                return;
            }

            if (loginForm.IsClosed == true)
            {
                loginForm.Show();
            }
            
        }

        private void MenuItemExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void MenuItemAbout_Click(object sender, EventArgs e)
        {
            AboutBox aboutMe = new AboutBox();
            aboutMe.Show();
        }

        private void MenuItemNew_Click(object sender, EventArgs e)
        {
            MakeNewNote();
        }

        private void MenuItemSave_Click(object sender, EventArgs e)
        {
            SaveNotes(true);
        }

        private void MenuItemSetting_Click(object sender, EventArgs e)
        {
            SettingsForm aboutMe = new SettingsForm();
            aboutMe.Show();
        }

        
        #endregion

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveNotes(false);
            Properties.Settings.Default.Save();
        }

    }
}
